import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyFram extends JFrame {
    //定义组件
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JTextField textField2;
    private JButton button;
    private JLabel label3;

    public MyFram(){
        //设置布局
        setLayout(new FlowLayout());

        //初始化
        label1 = new JLabel("Length:");
        textField1 = new JTextField();
        textField1.setColumns(10);
        label2 = new JLabel("Hight:");
        textField2 = new JTextField();
        textField2.setColumns(10);//用于设置文本框的可见列数，这里表示将这个文本框在界面上显示时，大致能呈现出容纳10等宽字符效果
        button = new JButton("cal");
        label3 = new JLabel("Area:");

        //添加组件
        //添加组件
        add(label1);
        add(textField1);
        add(label2);
        add(textField2);
        add(button);
        add(label3);

        //为按钮创建监听器
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                double length = Double.parseDouble(textField1.getText());
                //Double.paraseDoubles是将字符串形式表示的数字转换为双精度浮点数型
                double hight = Double.parseDouble(textField2.getText());
                double Area = (length * hight) / 2.0;
                label3.setText("Area: " + Area);


            }
        });
        //显示设置

        setSize(200, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }
}
